var database = require('../../../data/data.js');
Page({

  data: {
    flag: true,
    check_img: [
      { img: "../../../image/icon/check1.png" },
      { img: "../../../image/icon/check1.png" },
      { img: "../../../image/icon/check1.png" },
      { img: "../../../image/icon/check1.png" },
      { img: "../../../image/icon/check1.png" },
      { img: "../../../image/icon/check1.png" },
    ]
  },
  show: function () {
    this.setData({ flag: false })
  },

  hide: function () {
    this.setData({ flag: true })
  },
  onLoad: function (options) {
    var that = this;
    if (database.login == false) {
      wx.redirectTo({
        url: '/pages/newBinding/newBinding',
      })
    } else {
      wx.request({
        url: database.url1 + '/api/V2/user/currency',
        data: {
          accessToken: database.accessToken,
          userId: database.userId
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          that.setData({
            balance: res.data.data.currency
          })
        }
      });
      wx.request({
        url: 'https://51.5izuqiu.cn/api/V2/disburse/currency_list?accessToken=' + database.accessToken,
        method: 'POST',
        success: function (res) {
          var recharge = JSON.stringify(res.data.data);
          recharge = JSON.parse(recharge);
          var check_img = [];
          for (var i = recharge.currencyList.length - 1; i >= 0; i--) {
            var img = '';
            if (i == recharge.currencyList.length - 1) {
              img = { img: "../../../image/icon/check2.png" };
            } else img = { img: "../../../image/icon/check1.png" };
            check_img.push(img);
          }
          that.setData({
            recharge: res.data.data,
            check_img: check_img,
            check_cost: recharge.currencyList[0],
          });
        }
      })
    }
  },
  change_img: function (e) {
    var check = e.currentTarget.dataset.check
    var check_img = this.data.check_img;
    for (var i = this.data.recharge.currencyList.length - 1; i >= 0; i--) {
      if (check_img[i].img == "../../../image/icon/check2.png") {
        check_img[i].img = "../../../image/icon/check1.png"
        break;
      }
    }
    check_img[check].img = "../../../image/icon/check2.png"
    this.setData({
      check_img: check_img,
      check_cost: this.data.recharge.currencyList[check],
    })
  },
  step1: function () {
    var that = this;
    wx.login({
      success
      : function (res) {
        var code = res.code;
        if (code) {
          wx.request({
            url: database.url + '/project/step1.action',
            header: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
            method: 'get',
            data: {
              code: code
            },
            success: function (res) {
              var pages = getCurrentPages();
              var page = pages[pages.length - 1];
              page.setData({ bean1: res.data });
              that.step2();
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  step2: function () {
    var that = this
    var cost = this.data.check_cost.currency * 10;
    wx.request({
      url: database.url + '/project/step2.action?body=' + '我的足球' + '&total_fee=' + cost,
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'get',
      data: {
        openid: this.data.bean1.openid,
        costId: this.data.check_cost.costId,
        userId: database.userId,

      },
      success: function (res) {
        var pages = getCurrentPages();
        var page = pages[pages.length - 1];
        page.setData({ bean2: res.data });
        that.step3();
      }
    })
  },
  step3: function () {
    var that = this
    wx.request({
      url: database.url + '/project/step3.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'get',
      data: {
        prepay_id: this.data.bean2.prepay_id,
      },
      success: function (res) {
        var pages = getCurrentPages();
        var page = pages[pages.length - 1];
        page.setData({ bean3: res.data });
        that.step4();
      }
    })
  },
  step4: function () {
    var that = this
    wx.requestPayment({
      'timeStamp': this.data.bean3.timeStamp,
      'nonceStr': this.data.bean3.nonceStr,
      'package': 'prepay_id=' + this.data.bean2.prepay_id,
      'signType': 'MD5',
      'paySign': this.data.bean3.paySign,
      'success': function (res) {
        wx.showModal({
          title: '',
          content: '支付成功',
        })
      }
    })
  },
})